﻿#region License
// (c) Intergen.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TextGlow.Control.Utilities;

namespace TextGlow.Control.UI
{
  public partial class PageCover : UserControlExtended
  {
    //private double _contentsWidth = 200;
    //private double _contentsHeight = 200;

    public TranslateTransform TranslateTransformControl
    {
      get { return TranslateTransform; }
    }

    public PageCover()
    {
      InitializeComponent();
    }

    //public double ContentsHeight
    //{
    //  get { return _contentsHeight; }
    //  set { _contentsHeight = value; }
    //}

    //public double ContentsWidth
    //{
    //  get { return _contentsWidth; }
    //  set { _contentsWidth = value; }
    //}

    public void ChangeVideo(object sender, EventArgs e)
    {
      OnClick(e);
    }

    public Canvas Canvas
    {
      get { return (Canvas)Content; }
    }

    public StackPanel Container
    {
      get { return (StackPanel)Canvas.FindName("video0"); }
    }

    public Storyboard Animation
    {
      get { return CoverAnimations; }
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
      //Container.Width = _contentsWidth;
      //Container.Height = _contentsHeight;
      //Container.Clip = new RectangleGeometry() { Rect = new Rect(0, 0, _contentsWidth, _contentsHeight) };
      //Container.VerticalAlignment = VerticalAlignment;
      //Container.HorizontalAlignment = HorizontalAlignment;
      //FrameworkElementBase.UpdateLayout(Container);

      Container.Arrange(LayoutUtils.CreateRect(LayoutUtils.Zero, Container.DesiredSize));

      double scale = 1;
      if (Container.DesiredSize.Width > Width)
        scale = Width / Container.DesiredSize.Width;
      if (Container.DesiredSize.Height > Height)
        scale = Math.Min(scale, Height / Container.DesiredSize.Height);

      if (scale != 1)
        Container.RenderTransform = new ScaleTransform() { ScaleX = scale, ScaleY = scale };

      Size containerSize = Container.GetScaledSize();
      Point contentsLocation = new Point(0, (Height - containerSize.Height) / 2);

      //Size asdasd = Container.GetScaledSize();

      //Container.SetLocation(contentsLocation);
      //if (HorizontalAlignment == Alignment.

      return finalSize;
    }

    //protected override void PerformLayout()
    //{
    //  //Container.Width = _contentsWidth;
    //  //Container.Height = _contentsHeight;
    //  //Container.Clip = new RectangleGeometry() { Rect = new Rect(0, 0, _contentsWidth, _contentsHeight) };
    //  Container.VerticalAlignment = VerticalAlignment;
    //  Container.HorizontalAlignment = HorizontalAlignment;
    //  FrameworkElementBase.UpdateLayout(Container);

    //  FrameworkElement v = (FrameworkElement)Container;
    //  double scale = 1;
    //  if (v.Width > _contentsWidth)
    //    scale = _contentsWidth / v.Width;
    //  if (v.Height > _contentsHeight)
    //    scale = Math.Min(scale, _contentsHeight / v.Height);

    //  if (scale != 1)
    //    v.RenderTransform = new ScaleTransform() { ScaleX = scale, ScaleY = scale };

    //  Size containerSize = FrameworkElementBase.GetSize(v);
    //  FrameworkElementBase.SetLeft(v, (ContentsWidth - containerSize.Width) / 2);
    //  FrameworkElementBase.SetTop(v, (ContentsHeight - containerSize.Height) / 2);
    //  //if (HorizontalAlignment == Alignment.
    //}

    public void SetTranslation(Point position)
    {
      TranslateTransform.X = position.X;
      TranslateTransform.Y = position.Y;
    }

    public void SetName(string name)
    {
      SetValue(FrameworkElement.NameProperty, name);
      //Storyboard sb = (Storyboard)Canvas.Resources[0];
      //sb.SetValue(FrameworkElement.NameProperty, name);
    }
  }
}
